package confidence.run;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import weka.core.Attribute;
import weka.core.FastVector;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.converters.ArffSaver;
import confidence.helpers.Constants;
import confidence.helpers.PreprocessQM;
import confidence.helpers.Snapshot;
import confidence.helpers.Status;




public class RunMe {

	
	//Select person 
	private static String[] people = new String[]{"A", "B", "C", "D", "E"};
	
	//Select iteration (from 1 to 5)
	private static String[] iterations = new String[]{"01", "02", "03", "04", "05"};
	
	private static Snapshot snapshot = new Snapshot();
	
	

	public static void main(String[] args) {
		System.out.println("Starting...");
		Status.loadProperties ();
		
		String person = people[0];
		String iteration = iterations[0];
		
		//Read the selected person into an array
		ArrayList<String> lines = new ArrayList<String>();
		
		//read the dataset file
		FileInputStream dataset;
		DataInputStream in ;
		BufferedReader br;
		try 
		{
			//Insert the location of the data set
			dataset = new FileInputStream("data/sampleDataset.txt");
			in = new DataInputStream(dataset);
			br = new BufferedReader(new InputStreamReader(in));
			
			String strLine;
			while ((strLine = br.readLine()) != null)   {
				  
				if (strLine.startsWith(person+iteration));
				{
					lines.add(strLine);
				}
			}
			//Close the input stream
			in.close();
		} 
		catch (FileNotFoundException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		PreprocessQM prep = new PreprocessQM();
		
		int counter = 0;
		
		//CREATE ARFF 
		FastVector atts =  new FastVector();
		atts.addElement(new Attribute("chest_x"));
		atts.addElement(new Attribute("chest_y"));
		atts.addElement(new Attribute("chest_z"));
		atts.addElement(new Attribute("belt_x"));
		atts.addElement(new Attribute("belt_y"));
		atts.addElement(new Attribute("belt_z"));
		atts.addElement(new Attribute("ankleL_x"));
		atts.addElement(new Attribute("ankleL_y"));
		atts.addElement(new Attribute("ankleL_z"));
		atts.addElement(new Attribute("ankleR_x"));
		atts.addElement(new Attribute("ankleR_y"));
		atts.addElement(new Attribute("ankleR_z"));
		
		FastVector attClass = new FastVector();
		String[] classes = new String[]{"walking","falling","lying down","lying","sitting down","sitting","standing up from lying","on all fours","sitting on the ground","standing up from sitting","standing up from sitting on the ground"}; 
	    for (int i = 0; i < classes.length; i++)
	    {
	    	  attClass.addElement(classes[i]);
	   	}
	     
	    atts.addElement(new Attribute("activity", attClass));
	    Instances	data = new Instances("Ubisense_coordinates", atts, 0);
	    double[] vals = new double[data.numAttributes()];
	    
	    
		for (String snap : lines)
		{
			//create snapshots and print it out
			snapshot = prep.getSnapshot(snap);
			if (snapshot!=null)
			{
				vals = new double[data.numAttributes()];
				try
				{
					vals[0] = snapshot.tags[Constants.CHEST].coordRef.x;
					vals[1] = snapshot.tags[Constants.CHEST].coordRef.y;
					vals[2] = snapshot.tags[Constants.CHEST].coordRef.z;	
				}
				catch (Exception e) {
					vals[0] = Instance.missingValue();
					vals[1] =  Instance.missingValue();
					vals[2] =  Instance.missingValue();
				}

				try
				{
					vals[3] = snapshot.tags[Constants.BELT].coordRef.x;
					vals[4] = snapshot.tags[Constants.BELT].coordRef.y;
					vals[5] = snapshot.tags[Constants.BELT].coordRef.z;
				}
				catch (Exception e) {
					vals[3] = Instance.missingValue();
					vals[4] = Instance.missingValue();
					vals[5] = Instance.missingValue();
				}

				try
				{
					vals[6] = snapshot.tags[Constants.ANKLE_LEFT].coordRef.x;
					vals[7] = snapshot.tags[Constants.ANKLE_LEFT].coordRef.y;
					vals[8] = snapshot.tags[Constants.ANKLE_LEFT].coordRef.z;
				}
				catch (Exception e) {
					vals[6] = Instance.missingValue();
					vals[7] = Instance.missingValue();
					vals[8] = Instance.missingValue();
				}

				try
				{
					vals[9] = snapshot.tags[Constants.ANKLE_RIGHT].coordRef.x;
					vals[10] = snapshot.tags[Constants.ANKLE_RIGHT].coordRef.y;
					vals[11] = snapshot.tags[Constants.ANKLE_RIGHT].coordRef.z;
				}
				catch (Exception e) {
					vals[9] = Instance.missingValue();
					vals[10] = Instance.missingValue();
					vals[11] = Instance.missingValue();
				}
				
				vals[12] = attClass.indexOf(snapshot.trueClass);
				
				data.add(new Instance(1.0, vals));
				
				
			}
		}
		
		 ArffSaver saver = new ArffSaver();
		 saver.setInstances(data);

		 try {
			 saver.setFile(new File("./data/snapshots.arff"));
			 saver.writeBatch();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
	}

}
